package com.scpii.universal.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.FeedBackDto;
import com.scpii.universal.hibernate.dao.FeedBackManageDao;
import com.scpii.universal.hibernate.dao.FeedbackDao;
import com.scpii.universal.service.FeedBackManageService;

/**
 * 
 * universalWeb
 * 
 * @author zhangqiang 2012-7-10下午05:21:48
 */
@Service
public class FeedBackManageServiceImpl extends
		BaseServiceImpl<FeedBackDto, Integer> implements FeedBackManageService {
	@Resource
	private FeedBackManageDao feedBackManageDao;
	@Resource
	private FeedbackDao feedbackDao;

	@Resource
	public void setBaseDao(FeedBackManageDao feedBackManageDao) {
		super.setBaseDao(feedBackManageDao);
	}

	@Override
	public Integer getCountByCondition(FeedBackDto condition) {
		return feedbackDao.getCountByCondition(condition);
	}

	@Override
	public List<FeedBackDto> geFeedBackList(FeedBackDto condition,
			Integer pageNo, Integer pageSize) {
		return feedbackDao.geFeedBackList(condition, pageNo, pageSize);
	}

	/**
	 * 获取留言额详细信息
	 * 
	 * @return
	 * @author zhangqiaing 2012-7-20上午10:37:36
	 */
	public List<FeedBackDto> getFeedBackDetailInfo(Integer intFeedbackId,
			Long intUserId,Integer intAppId) {
		return feedbackDao.getFeedBackDetailInfo(intFeedbackId, intUserId, intAppId);

	}

	/**
	 * 取得留言版顯示的信息
	 * 
	 * @return
	 * @author zhangqiaing 2012-7-20上午10:37:36
	 */
	public List<FeedBackDto> getContentDetailInfo(List<FeedBackDto> feedBackList) {

		if (feedBackList == null) {
			return null;
		}

		List<FeedBackDto> contentList = new ArrayList<FeedBackDto>();

		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.FeedBackDto(t1.intAdminId,");
		hql.append(" t1.intFeedbackId , ");
		hql.append(" t1.intParentFeedbackId , ");
		hql.append(" t1.intUserId , ");
		hql.append(" t1.intAppId , ");
		hql.append(" t1.varFeedbackTitle , ");
		hql.append(" t1.varFeedbackContent , ");
		hql.append(" t1.datFeedbackDatetime , ");
		hql.append(" t1.intFeedbackType , ");
		hql.append(" t1.intReplyStatus , ");
		hql.append(" t1.datReplyDatetime , ");
		hql.append(" t2.varLoginName as varUserName, ");
		hql.append(" t2.varPhonenum) ");
		hql.append(" from FeedbackTab t1 ,");	
		hql.append("  ManageUserTab t2 ");	
		hql.append(" where t1.intParentFeedbackId =:intParentFeedbackId ");
		hql.append(" and t1.intAdminId = t2.intUserId");
		hql.append(" and t1.intDeleteSign =:intDeleteSign");
		hql.append(" and t2.intDeleteSign =:intDeleteSign");
		hql.append(" order by t1.datFeedbackDatetime ");
		
		Map<String, Object> argsMap = new HashMap<String, Object>();
		// argsMap.put("intUserId", intUserId);
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		List<FeedBackDto> tempList;
		for (FeedBackDto backDto : feedBackList) {
			contentList.add(backDto);
			argsMap.put("intParentFeedbackId", backDto.getIntFeedbackId());
			tempList=this.hqlGetList(hql.toString(), argsMap);
			
			contentList.addAll(tempList);
		}

		return contentList;
//		return feedbackDao.getContentDetailInfo(feedBackList);
	}

	@Override
	public void delFeedBackInfo(Integer intFeedbackId) {
		StringBuffer hql = new StringBuffer("update FeedbackTab f set ");
		hql.append("f.intDeleteSign=:intDeleteSign");
		hql.append(",f.datUpdateDatetime=:datUpdateDatetime");
		hql.append(" where f.intFeedbackId=:intFeedbackId");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_DELETED);
		argsMap.put("datUpdateDatetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		argsMap.put("intFeedbackId", intFeedbackId);
		feedBackManageDao.hqlOperation(hql.toString(), argsMap);
	}
}
